/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * usrStartup_1.java
 *
 * Created on 2011-5-4, 13:59:54
 */
package com.edu.kamai.client;

import com.edu.kamai.client.control.usrControl;
import com.edu.kamai.config.Constant;
import com.edu.kamai.utility.MD5;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class usrStartup_1 extends javax.swing.JFrame {

    /** Creates new form usrStartup_1 */
    public usrStartup_1() {
        initComponents();
        setNet();
    }

    private void setNet(){
        try{
            InputStream in = getClass().getResourceAsStream(Constant.netiniFilePath);
            Scanner scan = new Scanner(in);
            List<String> netList = new ArrayList<String>();
            
            while(scan.hasNextLine()){
                String line = scan.nextLine();
                if(!line.equals("")){
                    if(!line.startsWith("#")){
                        netList.add(line);
                    }
                }
            }
            usrControl.setNetList(netList);
            
            scan.close();
            in.close();
            
        }catch(Exception ex){
            ex.printStackTrace();
            //Control.setNetList(new ArrayList<String>());
        }
        usrControl.setServer();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        T_usrID_1 = new javax.swing.JTextField();
        B_Login = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        T_usrPwd_1 = new javax.swing.JPasswordField();
        l_student = new javax.swing.JRadioButton();
        l_teacher = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        T_rUsrPwd = new javax.swing.JPasswordField();
        T_usrPwd = new javax.swing.JPasswordField();
        T_emailAdr = new javax.swing.JTextField();
        T_usrID = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        B_register = new javax.swing.JButton();
        r_student = new javax.swing.JRadioButton();
        r_teacher = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        B_Login.setText("Login");
        B_Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_LoginActionPerformed(evt);
            }
        });

        jLabel6.setText("User ID");

        jLabel7.setText("PassWord");

        buttonGroup1.add(l_student);
        l_student.setText("student");

        buttonGroup1.add(l_teacher);
        l_teacher.setText("teacher");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(l_teacher, 0, 0, Short.MAX_VALUE)
                    .addComponent(l_student, 0, 0, Short.MAX_VALUE)
                    .addComponent(T_usrPwd_1)
                    .addComponent(T_usrID_1, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                    .addComponent(B_Login, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(106, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(T_usrID_1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(T_usrPwd_1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(l_student)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(l_teacher)
                        .addGap(33, 33, 33)
                        .addComponent(B_Login))
                    .addComponent(jLabel7))
                .addContainerGap(85, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Login", jPanel2);

        jLabel4.setText("Again");

        jLabel5.setText("Email");

        jLabel2.setText("User ID");

        jLabel3.setText("PassWord");

        B_register.setText("Register");
        B_register.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_registerActionPerformed(evt);
            }
        });

        buttonGroup2.add(r_student);
        r_student.setText("student");

        buttonGroup2.add(r_teacher);
        r_teacher.setText("teacher");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(B_register)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(r_teacher)
                            .addContainerGap())
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(r_student)
                                .addContainerGap())
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(T_rUsrPwd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                                    .addComponent(T_usrPwd, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                                    .addComponent(T_usrID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                                    .addComponent(T_emailAdr, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
                                .addGap(107, 107, 107))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(T_usrID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(T_usrPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(T_rUsrPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(T_emailAdr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(8, 8, 8)
                .addComponent(r_student)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(r_teacher)
                .addGap(18, 18, 18)
                .addComponent(B_register)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Register", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void showMessage(int ans,String msg){
        if(ans == 1){
            javax.swing.JOptionPane.showMessageDialog(this, msg, "Done", ans);
        }else{
            javax.swing.JOptionPane.showMessageDialog(this, msg, "Error", ans);
        }
    }
    
    private void B_registerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_registerActionPerformed
        String usrID = T_usrID.getText();
        String usrPwd = T_usrPwd.getText();
        String rusrPwd = T_rUsrPwd.getText();
        //System.out.println(usrPwd + " " + rusrPwd);
        String usrEmailAdr = T_emailAdr.getText();
        if(usrID.equals("") || usrID == null || usrPwd.equals("") || usrPwd == null
                || rusrPwd.equals("") || rusrPwd == null || usrEmailAdr.equals("") || usrEmailAdr == null){
            //javax.swing.JOptionPane.showMessageDialog(this, "Please fill out all information ", "Error", 0);
            showMessage(0,"Please fill out all information!");
            T_usrPwd.setText("");
            T_rUsrPwd.setText("");
            return;
        }
        if(!usrPwd.equals(rusrPwd)){
            //javax.swing.JOptionPane.showMessageDialog(this, "The two passwords are inconsistent ", "Error", 0);
            showMessage(0,"The two passwords are inconsistent!");
            T_usrPwd.setText("");
            T_rUsrPwd.setText("");
            return;
        }
        String usrPwdMd5 = MD5.Md5(usrPwd);
        int role = -1;
        if(r_student.isSelected() == true){
            role = Constant.KAMAI_STUDENT_ROLE;
        }else if(r_student.isSelected() == true){
            role = Constant.KAMAI_TEACHER_ROLE;
        }
        int ans = usrControl.register(usrID, usrPwdMd5, usrEmailAdr, role);
        showMessage(ans, usrControl.getMessage());
        
}//GEN-LAST:event_B_registerActionPerformed

    private void B_LoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_LoginActionPerformed
        // TODO add your handling code here:
        String usrID = T_usrID_1.getText();
        String usrPwd = T_usrPwd_1.getText();
        if(usrID.equals("") || usrID == null || usrPwd.equals("") || usrPwd == null){
            //javax.swing.JOptionPane.showMessageDialog(this, "Please fill out all information ", "Error", 0);
            showMessage(0,"Please fill out all information!");
            T_usrID_1.setText("");
            T_usrPwd_1.setText("");
            return;
        }
        String usrPwdMd5 = MD5.Md5(usrPwd);
        int role = -1;
        if(l_student.isSelected() == true){
            role = Constant.KAMAI_STUDENT_ROLE;
        }else if(l_teacher.isSelected() == true){
            role = Constant.KAMAI_TEACHER_ROLE;
        }
        int ans = usrControl.login(usrID, usrPwdMd5, role);
        if(ans == 1){
            //javax.swing.JOptionPane.showMessageDialog(this, , "Done", 1);
            showMessage(ans, usrControl.getMessage());
            if(role == Constant.KAMAI_TEACHER_ROLE){
                new TeacherMain(usrID).setVisible(true);
                this.setVisible(false);
            }else if (role == Constant.KAMAI_STUDENT_ROLE){
                new StudentMain(usrID).setVisible(true);
                this.setVisible(false);
            }
        }else {
            showMessage(ans, usrControl.getMessage());
            //javax.swing.JOptionPane.showMessageDialog(this, usrControl.getMessage(), "Error", 0);
        }
        clearAll();
}//GEN-LAST:event_B_LoginActionPerformed

    private void clearAll(){
        T_emailAdr.setText("");
        T_rUsrPwd.setText("");
        T_usrID.setText("");
        T_usrID_1.setText("");
        T_usrPwd.setText("");
        T_usrPwd_1.setText("");
        l_student.setSelected(true);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new usrStartup_1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton B_Login;
    private javax.swing.JButton B_register;
    private javax.swing.JTextField T_emailAdr;
    private javax.swing.JPasswordField T_rUsrPwd;
    private javax.swing.JTextField T_usrID;
    private javax.swing.JTextField T_usrID_1;
    private javax.swing.JPasswordField T_usrPwd;
    private javax.swing.JPasswordField T_usrPwd_1;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JRadioButton l_student;
    private javax.swing.JRadioButton l_teacher;
    private javax.swing.JRadioButton r_student;
    private javax.swing.JRadioButton r_teacher;
    // End of variables declaration//GEN-END:variables
}
